#include "partitionwidget.h"

PartitionWidget::PartitionWidget(Partition partition, int screen_id, BaseShowWidget *parent) : 
                                 BaseShowWidget(parent), m_screen_id(screen_id), m_partition(partition)
{
    //为每一个partition注册点击事件回调函数，通过全局观察者发送消息
    GlobalObserver::getGlobalObserver()->attach(loss_select_msg, this, SLOT(setLossSelect()));

    m_stack_layout = new QStackedLayout(this);
    this->setContentsMargins(0, 0, 0, 0);

    for (auto it = m_partition.layouts.begin(); it != m_partition.layouts.end(); it++)
        addLayout(it.value());

    updatePartition(partition);
}

void PartitionWidget::updatePartition(Partition partition)
{
    m_partition = partition;

    QString qss = styleSheet();
    if (partition.common.background_color != "")
        replaceQss(qss, "background-color:", QString("background-color:#%1;").arg(partition.common.background_color));
    else
        replaceQss(qss, "background-color:", "background-color:transparent;");
    setStyleSheet(qss);

    clear();
    QPixmap pixmap(g_pro_data.path + QString("/res/") + partition.common.background);
    setPixmap(pixmap);
    setScaledContents(true);

    Common this_common = partition.common;
    setGeometry(this_common.x, this_common.y, this_common.width, this_common.height);

    for (auto it = partition.layouts.begin(); it != partition.layouts.end(); it++)
    {
        m_layouts_data[it.key()].layout_data = it.value();
        m_layouts_data[it.key()].layout_widget->updateLayout(it.value());
    }
}

void PartitionWidget::setSelectStyle(bool select)//设置控件选择样式
{
    if (select)
    {

//此处想点击partition的时候默认选中他的layout，取消这一设定
//        if (m_stack_layout->currentWidget() != NULL)
//            m_stack_layout->currentWidget()->setStyleSheet("background-color:rgb(225,255,255);border:2px solid orange;");
//        else
        setStyleSheet(styleSheet().append("border:2px solid green;"));

        GlobalObserver::getGlobalObserver()->notify(partition_select_msg, m_screen_id, m_partition.id);
        raise();
    }
    else
    {
        //setStyleSheet("border:none;background-color:transparent;");
    }
}

void PartitionWidget::setAllChildrenLossSelect()
{
    setLossSelect();
    for (auto it = m_layouts_data.begin(); it != m_layouts_data.end(); it++)
        it.value().layout_widget->setAllChildrenLossSelect();
}

int PartitionWidget::setCurrentChangedLayoutFromId(int layout_id)
{
    for (int i = 0; i < m_stack_layout->count(); i++)
    {
        LayoutWidget *curr_widget = (LayoutWidget *)m_stack_layout->widget(i);
        if (curr_widget->getLayoutId() == layout_id)
        {
            m_stack_layout->setCurrentIndex(i);
            m_stack_layout->currentWidget()->setStyleSheet("background-color:rgb(225,255,255);border:2px solid orange");
            GlobalObserver::getGlobalObserver()->notify(layout_select_msg, m_screen_id, m_partition.id, layout_id);
            raise();
        }
    }
    return 0;
}

void PartitionWidget::delLayout(int layout_id)
{
    delete m_layouts_data[layout_id].layout_widget;
    m_layouts_data[layout_id].layout_widget = NULL;

    m_partition.layouts.remove(layout_id);
    m_layouts_data.remove(layout_id);
}
void PartitionWidget::addLayout(Layout one_layout)
{
    LayoutShowData one_data;
    one_data.layout_data = one_layout;
    one_data.layout_widget = new LayoutWidget(one_layout, m_screen_id, m_partition.id, this);

    int new_index = m_stack_layout->addWidget(one_data.layout_widget);
    m_stack_layout->setCurrentIndex(new_index);

    m_layouts_data[one_layout.id] = one_data;

    one_data.layout_widget->setSelected();
    one_data.layout_widget->show();
}
